package com.james.customcamera.model;

import com.james.customcamera.CustomCameraApplication;
import com.james.customcamera.base.BaseModel;
import com.james.customcamera.bean.request.RegisterRequest;
import com.james.customcamera.exception.ApiException;
import com.james.customcamera.observer.CommonObserver;
import com.james.customcamera.transformer.CommonTransformer;
import com.james.customcamera.utils.Md5Util;


public class RegisterModel extends BaseModel {
    public void register(String userName, String password, String nickName, final DataListener listener) {
        if (listener == null) {
            throw new RuntimeException("InfoHint不能为空");
        }
        RegisterRequest request = new RegisterRequest();
        request.setUserName(userName);
        request.setPassword(Md5Util.toMd5(password));
        request.setNickName(nickName);
        httpService.register(request)
                .compose(new CommonTransformer<String>())
                .subscribe(new CommonObserver<String>(CustomCameraApplication.getContext()) {
                    @Override
                    public void onNext(String registerBean) {
                        listener.successInfo(registerBean);
                    }

                    @Override
                    protected void onError(ApiException e) {
                        super.onError(e);
                        listener.failInfo(e.message);
                    }
                });
    }


}
